package com.kaikai.aclservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaikai.aclservice.entity.AclPermission;
import com.kaikai.aclservice.entity.AclRolePermission;
import com.kaikai.aclservice.mapper.AclPermissionMapper;
import com.kaikai.aclservice.service.AclPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaikai.aclservice.service.AclRolePermissionService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author yujiankai
 * @since 2021-07-10
 */
@Service
public class AclPermissionServiceImpl extends ServiceImpl<AclPermissionMapper, AclPermission> implements AclPermissionService {

    @Autowired
    private AclRolePermissionService rolePermissionService;

    @ApiModelProperty(value = "封装所有数据")
    @Override
    public List<AclPermission> selectList(List<AclPermission> permissionList) {
        //用于封装所有数据
        List<AclPermission> trees = new ArrayList<>();

        //1.查询一层数据,设置level=1
        for (AclPermission permission : permissionList) {
            //得到入口
            if ("0".equals(permission.getPid())) {
                permission.setLevel(1);
                QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
                wrapper.eq("pid", 1);
                trees.add(findChildren(permission, permissionList));
            }
        }
        return trees;
    }

    @ApiModelProperty(value = "递归删除菜单")
    public void removeChildrenById(String id) {

        List<String> idList = new ArrayList<>();
        //递归得到所有数据
        idList.add(id);
        idList = selectChildIdListById(id,idList);
        this.removeByIds(idList);
    }


//递归得到id
    public List<String> selectChildIdListById(String id,List<String> idList) {

        //得到下层id集合
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        wrapper.select("id");
        List<AclPermission> childIdList = this.list(wrapper);

        if(childIdList!=null){
            for(AclPermission permission:childIdList){
                //把childIdList中的数据取出来,封装到idList中
                idList.add(permission.getId());
                //递归
                selectChildIdListById(permission.getId(), idList);
            }
        }
        return idList;



    }

    @ApiModelProperty(value = "递归查询子节点")
    public static AclPermission findChildren(AclPermission permission, List<AclPermission> permissionList) {
        //设置父节点的子节点
        //初始化
        permission.setChildren(new ArrayList<AclPermission>());
        for (AclPermission it : permissionList) {
            if (permission.getId().equals(it.getPid())) {
                int level = permission.getLevel() + 1;
                it.setLevel(level);
                permission.getChildren().add(findChildren(it, permissionList));
            }
        }
        return permission;
    }

//====================根据id递归删除=====================
    public void deleteById(String id) {
        //删除头
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        this.remove(wrapper);
//得到子
        QueryWrapper<AclPermission> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("pid", id);
        List<AclPermission> list = this.list(wrapper1);
        //递归删除子
        if (list != null) {
            for (AclPermission permission : list) {
                try {
                    this.remove(wrapper1);
                } catch (Exception e) {
                }
                this.deleteById(permission.getId());
            }
        }
    }

    /**
     * 给用户添加权限
     * @param roleId
     * @param permissionIds
     */
    @Override
    public void saveRolePermissionRealtionShip(String roleId, String[] permissionIds) {
        List<AclRolePermission> rolePermissionList=new ArrayList<>();
        for (String permissionId:permissionIds) {
            AclRolePermission rolePermission=new AclRolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        rolePermissionService.saveBatch(rolePermissionList);

    }
}
